# from rest_framework.generics import (
#     ListAPIView,
#     ListCreateAPIView,
#     RetrieveAPIView,
#     RetrieveUpdateAPIView,
#     RetrieveUpdateDestroyAPIView,
# )

from django.contrib.auth.mixins import LoginRequiredMixin
from django.shortcuts import get_object_or_404
from django.urls import reverse_lazy

from django.views.generic import (
    CreateView,
    DeleteView,
    DetailView,
    ListView,
    RedirectView,
    UpdateView,
)

from .forms import TagForm, StartupForm, NewsLinkForm
from .models import Tag, Startup, NewsLink

from .view_mixins import (
    NewsLinkContextMixin,
    NewsLinkObjectMixin,
    VerifyStartupFkToUriMixin,
)

class TagList(ListView):
    """Display a list of Tags"""

    queryset = Tag.objects.all()
    template_name = "tag/list.html"

class TagCreate(CreateView):
    """Create new Tags via HTML form"""

    form_class = TagForm
    model = Tag
    template_name = "tag/form.html"
    extra_context = {"update": False}

class TagUpdate(UpdateView):
    """Update a Tag via HTML form"""

    form_class = TagForm
    model = Tag
    template_name = "tag/form.html"
    extra_context = {"update": True}

class TagDelete(DeleteView):
    """Confirm and delete a Tag via HTML Form"""

    form_class = TagForm
    model = Tag
    template_name = "tag/confirm_delete.html"
    success_url = reverse_lazy("tag_list")

class TagDetail(DetailView):
    """Display a single Tag"""

    queryset = Tag.objects.all()
    template_name = "tag/detail.html"

class StartupList(ListView):
    """Display a list of Startups"""

    queryset = Startup.objects.all()
    template_name = "startup/list.html"

class StartupCreate(LoginRequiredMixin, CreateView):
    """Create new Startups via HTML form"""

    form_class = StartupForm
    model = Startup
    template_name = "startup/form.html"
    extra_context = {"update": False}


class StartupDetail(DetailView):
    """Display a single Startup"""

    queryset = Startup.objects.all()
    template_name = "startup/detail.html"

class StartupUpdate(LoginRequiredMixin, UpdateView):
    """Update a Startup via HTML form"""

    form_class = StartupForm
    model = Startup
    template_name = "startup/form.html"
    extra_context = {"update": True}

class StartupDelete(LoginRequiredMixin, DeleteView):

    model = Startup
    template_name = "startup/confirm_delete.html"
    success_url = reverse_lazy("startup_list")

class NewsLinkCreate(
    VerifyStartupFkToUriMixin,
    NewsLinkContextMixin,
    LoginRequiredMixin,
    CreateView,
):
    """Create a link to an article about a startup"""

    extra_context = {"update": False}
    form_class = NewsLinkForm
    model = NewsLink
    template_name = "newslink/form.html"

    def get_initial(self):
        """Pre-select Startup in NewsLinkForm"""
        startup = get_object_or_404(
            Startup, slug=self.kwargs.get("startup_slug")
        )
        return dict(
            super().get_initial(), startup=startup.pk
        )

class NewsLinkDetail(NewsLinkObjectMixin, RedirectView):
    """Redirect to Startup Detail Page"""

    def get_redirect_url(self, *args, **kwargs):
        """Redirect user to Startup page"""
        return self.get_object().get_absolute_url()

class NewsLinkUpdate(
    VerifyStartupFkToUriMixin,
    NewsLinkObjectMixin,
    NewsLinkContextMixin,
    LoginRequiredMixin,
    UpdateView,
):
    """Update a link to an article about a startup"""

    extra_context = {"update": True}
    form_class = NewsLinkForm
    template_name = "newslink/form.html"

class NewsLinkDelete(
    NewsLinkObjectMixin,
    NewsLinkContextMixin,
    LoginRequiredMixin,
    DeleteView,
):
    """Delete a link to an article about a startup"""

    template_name = "newslink/confirm_delete.html"

    def get_success_url(self):
        """Return the detail page of the Startup parent"""

        startup = get_object_or_404(
            Startup, slug=self.kwargs.get("startup_slug")
        )
        return startup.get_absolute_url()

# class TagAPIDetail(RetrieveUpdateDestroyAPIView):
#     """Return JSON for single Tag Object"""
#
#     queryset = Tag.objects.all()
#     serializer_class = TagSerializer
#     lookup_field = "slug"
#
# class TagAPIList(ListCreateAPIView):
#     """Return JSON for multiple Tag objects"""
#
#     queryset = Tag.objects.all()
#     serializer_class = TagSerializer
#
# class StartupAPIDetail(RetrieveAPIView):
#     """Return JSON for single Startup object"""
#
#     queryset = Startup.objects.all()
#     serializer_class = StartupSerializer
#     lookup_field = "slug"
#
# class StartupAPIList(ListAPIView):
#     """Return JSON for multiple Startup objects"""
#
#     queryset = Startup.objects.all()
#     serializer_class = StartupSerializer
#
# class NewsLinkAPIList(ListAPIView):
#     """Return JSON for Multiple NewsLink Objects"""
#
#     queryset = NewsLink.objects.all()
#     serializer_class = NewsLinkSerializer
#
# class NewsLinkAPIDetail(RetrieveAPIView):
#     """Return JSON for single NewsLink object"""
#
#     queryset = NewsLink.objects.all()
#     serializer_class = NewsLinkSerializer
#
#     def get_object(self):
#         """Override Django Rest Framework's generic method"""
#
#         startup_slug = self.kwargs.get("startup_slug")
#         newslink_slug = self.kwargs.get("newslink_slug")
#
#         queryset = self.filter_queryset(self.get_queryset())
#
#         newslink = get_object_or_404(
#             queryset,
#             slug=newslink_slug,
#             startup__slug=startup_slug,
#         )
#         self.check_object_permissions(
#             self.request, newslink
#         )
#         return newslink
#

